
/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名:SessionInterceptor.java
 * <p>
 * 类产生时间: 2012-5-23 下午4:27:57
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.interceptor;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.domain.SysUser;

public class SessionInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {
		// Object obj = (SysUser)
		// request.getSession().getAttribute(Constant.LOGIN_USER);
		// if (null == obj) {
		// outputScript(response, "");
		// }
		// System.out.println("AFTER");
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView model)
			throws Exception {
		Object obj = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		if (null == obj) {
			outputScript(response, "");
		}
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		Object obj = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		if (null != obj) {
			return true;
		} else {
			outputScript(response, "");
			return false;
		}
	}

	private void outputScript(HttpServletResponse response, String root) throws IOException {
		StringBuffer impjs = new StringBuffer();
		impjs.append("<!DOCTYPE html><html>");
		impjs.append("<script type=\"text/javascript\" src=\"/gabzjl/resource/js/jquery-1.7.2.min.js\"></script>");
		impjs.append("<script type=\"text/javascript\" src=\"/gabzjl/resource/artdialog/artdialog.js?skin=red\"></script>");
		impjs.append("<script type=\"text/javascript\" src=\"/gabzjl/resource/artdialog/iframetools.js\"></script>");
//		impjs.append("<script type=\"text/javascript\">art.dialog('sss');</script>");
//		impjs.append("<script>art.dialog.open({title:'sss'});</script>");
		impjs.append("</head><body></body></html>");
		response.setContentType("text/html;charset=utf-8");
		Writer writer = response.getWriter();
		writer.write(impjs.toString());
		writer.flush();
		writer.close();

	}

	private String getScript() {
		StringBuffer impjs = new StringBuffer();
		impjs.append("<!DOCTYPE html><html>");
		impjs.append("<script src=\"/gabzjl/resource/js/jquery-1.7.2.min.js\"></script>");
		impjs.append("<script src=\"/gabzjl/resource/artdialog/artdialog.js?skin=red\"></script>");
		impjs.append("<script src=\"/gabzjl/resource/artdialog/iframetools.js\"></script>");
		impjs.append("<script>art.dialog({title:'sss'});</script>");
		impjs.append("</head><body></body></html>");
		return impjs.toString();
	}

}
